package hu.bme.aut.consulttime.client.components;

import hu.bme.aut.consulttime.client.TeamService;
import hu.bme.aut.consulttime.client.TeamServiceAsync;
import hu.bme.aut.consulttime.entitydtos.ProjectDTO;
import hu.bme.aut.consulttime.entitydtos.TeamDTO;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ClickEvent;

public class CreateTeam extends DialogBox {
	
	private TeamServiceAsync teamService = GWT
			.create(TeamService.class);
	
	private TextBox textBoxName;
	private ListBox comboBoxProject;
	private ListBox comboBoxSemester;
	
	public CreateTeam(List<ProjectDTO> projectDTOs, List<String> semesters) {
		super(true);
		FlexTable flexTable = new FlexTable();

		Label lblNewLabel = new Label("Create New Team");
		flexTable.setWidget(0, 0, lblNewLabel);

		Label lblProjectTitle = new Label("Team Name");
		flexTable.setWidget(1, 0, lblProjectTitle);

		textBoxName = new TextBox();
		flexTable.setWidget(1, 1, textBoxName);
		textBoxName.setWidth("100%");
		
		// --------------------- Upload the Projects ---------------------
		Label lblProjectType = new Label("Project Name (Type)");
		flexTable.setWidget(2, 0, lblProjectType);

		comboBoxProject = new ListBox();
		flexTable.setWidget(2, 1, comboBoxProject);
		comboBoxProject.setWidth("100%");
		
		for(ProjectDTO tmp : projectDTOs){
			comboBoxProject.addItem(tmp.getTitle() + " (" + tmp.getType() + ")" + " -id#" + tmp.getProjectId());
		}
		// -- Add Semesters
		Label lblProjectSemester = new Label("Semester");
		flexTable.setWidget(3, 0, lblProjectSemester);
		
		comboBoxSemester = new ListBox();
		flexTable.setWidget(3, 1, comboBoxSemester);
		comboBoxSemester.setWidth("100%");
		
		for(String tmp : semesters){
			comboBoxSemester.addItem(tmp);
		}
		
		
		setWidget(flexTable);
		
		Button btnCreate = new Button("Create");
		btnCreate.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				TeamDTO teamDTO = new TeamDTO();
				teamDTO.setName(textBoxName.getText());
				
				teamDTO.setSemesterId(comboBoxSemester.getValue(comboBoxSemester.getSelectedIndex()));
				
				//Project id
				String chosen = comboBoxProject.getValue(comboBoxProject.getSelectedIndex());
				String id = chosen.substring(chosen.lastIndexOf("#")+1);
				teamDTO.setProjectId(Long.parseLong(id));
				
				teamService.createTeam(teamDTO, new AsyncCallback<Boolean>() {
					
					@Override
					public void onSuccess(Boolean result) {
						if(result == true){
							Window.confirm("'"+textBoxName.getText() + "' team created!");
					        //Refresh the table
					        TeamPage.loadTeams();
						}else{
							Window.alert("Team creation failed!");
						}
					}
					
					@Override
					public void onFailure(Throwable caught) {
						Window.alert("Remote Procedure Call - Failure");
					}
				});
			}
		});
		flexTable.setWidget(4, 1, btnCreate);
	}

}
